package cn.edu.scau.cmi.huangxiyan.client;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import cn.edu.scau.cmi.huangxiyan.composite.ConsistentComponent;
import cn.edu.scau.cmi.huangxiyan.composite.ConsistentComposite;
import cn.edu.scau.cmi.huangxiyan.composite.ConsistentLeaf;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ConsistentCompositeClient extends Application {

	public Set<ConsistentComponent> consistentLeaf = new HashSet<ConsistentComponent>();
	public Set<ConsistentComponent> consistentComposite = new HashSet<ConsistentComponent>();
	public TextArea textArea = new TextArea("");

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		primaryStage.setTitle("ConsistentCompositeClient");
		VBox vBox = new VBox(15);
		vBox.setPadding(new Insets(15, 5, 5, 5));

		Label personLabel = new Label("个人姓名：");
		personLabel.setPrefSize(80, 25);

		TextField personTextField = new TextField("请输入个人姓名");
		personTextField.setPrefSize(520, 25);

		HBox personHBox = new HBox(5);
		personHBox.setPadding(new Insets(5, 0, 5, 0));
		personHBox.getChildren().addAll(personLabel, personTextField);

		Label teamLabel = new Label("团体1名称：");
		teamLabel.setPrefSize(80, 25);

		TextField teamTextField = new TextField("请输入团体1名称");
		teamTextField.setPrefSize(520, 25);

		HBox teamHBox = new HBox(5);
		teamHBox.setPadding(new Insets(5, 0, 5, 0));
		teamHBox.getChildren().addAll(teamLabel, teamTextField);

		Label team2Label = new Label("团体2名称：");
		team2Label.setPrefSize(80, 25);

		TextField team2TextField = new TextField("请输入团体2名称");
		team2TextField.setPrefSize(520, 25);

		HBox team2HBox = new HBox(5);
		team2HBox.setPadding(new Insets(5, 0, 5, 0));
		team2HBox.getChildren().addAll(team2Label, team2TextField);

		textArea.setPrefSize(600, 200);
		textArea.setEditable(false);

		Button viewAllButton = new Button("显示全部");
		viewAllButton.setPrefSize(300, 25);
		viewAllButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				textArea.setText("");

				for (ConsistentComponent c : consistentComposite) {
					if (((ConsistentComposite) c).getParent() == null) {
						viewComposite(c, 1);
					}
				}

				for (ConsistentComponent c : consistentLeaf) {
					if (((ConsistentLeaf) c).getParent() == null) {
						textArea.appendText("-个人：" + ((ConsistentLeaf) c).getName() + "\n");
					}
				}

				if (textArea.getText().equals("")) {
					textArea.appendText("无任何个人或团队！\n");
				}
			}
		});

		Button createPersonButton = new Button("创建个人");
		createPersonButton.setPrefSize(300, 25);
		createPersonButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				textArea.setText("");

				ConsistentLeaf leaf = new ConsistentLeaf();
				leaf.setName(personTextField.getText());

				if (consistentLeaf.contains(leaf)) {
					textArea.appendText("个人 " + leaf.getName() + " 已存在，无法创建！");
				} else {
					consistentLeaf.add(leaf);
					textArea.appendText("创建个人 " + leaf.getName() + " 成功！");
				}
			}
		});

		Button createTeamButton = new Button("创建团队1");
		createTeamButton.setPrefSize(300, 25);
		createTeamButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				textArea.setText("");

				ConsistentComposite composite = new ConsistentComposite();
				composite.setName(teamTextField.getText());

				if (consistentComposite.contains(composite)) {
					textArea.appendText("团队1 " + composite.getName() + " 已存在，无法创建！");
				} else {
					consistentComposite.add(composite);
					textArea.appendText("创建团队1 " + composite.getName() + " 成功！");
				}
			}
		});

		Button addPersonToTeamButton = new Button("添加个人到团队1");
		addPersonToTeamButton.setPrefSize(150, 25);
		addPersonToTeamButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				textArea.setText("");

				ConsistentComposite composite = new ConsistentComposite();
				composite.setName(teamTextField.getText());
				Iterator<ConsistentComponent> iterator = consistentComposite.iterator();
				while (iterator.hasNext()) {
					ConsistentComposite temp = (ConsistentComposite) iterator.next();
					if (temp.getName().equals(composite.getName())) {
						composite = temp;
						break;
					}
				}

				ConsistentLeaf leaf = new ConsistentLeaf();
				leaf.setName(personTextField.getText());
				iterator = consistentLeaf.iterator();
				while (iterator.hasNext()) {
					ConsistentLeaf temp = (ConsistentLeaf) iterator.next();
					if (temp.getName().equals(leaf.getName())) {
						leaf = temp;
						break;
					}
				}

				if (consistentComposite.contains(composite)) {
					if (consistentLeaf.contains(leaf)) {
						if (leaf.getParent() == null) {
							consistentComposite.remove(composite);
							composite.addChild(leaf);
							consistentComposite.add(composite);
							consistentLeaf.remove(leaf);
							leaf.setParent(composite);
							consistentLeaf.add(leaf);
							textArea.appendText("添加个人 " + leaf.getName() + " 到团队1 " + composite.getName() + " 成功！");
						} else if (leaf.getParent().getName().equals(composite.getName())) {
							textArea.appendText("个人 " + leaf.getName() + " 已加入团队1 " + composite.getName() + " ，无法添加！");
						} else {
							textArea.appendText("个人 " + leaf.getName() + " 已加入其它团队 " + composite.getName() + " ，无法添加！");
						}
					} else {
						textArea.appendText("个人 " + leaf.getName() + " 不存在，请先创建再进行添加操作！");
					}
				} else {
					textArea.appendText("团队1 " + composite.getName() + " 不存在，请先创建再进行添加操作！");
				}
			}
		});

		Button addTeamToTeamButton = new Button("添加团队1到团队2");
		addTeamToTeamButton.setPrefSize(150, 25);
		addTeamToTeamButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				textArea.setText("");

				if (!(team2TextField.getText().equals(teamTextField.getText()))) {
					ConsistentComposite composite2 = new ConsistentComposite();
					composite2.setName(team2TextField.getText());
					ConsistentComposite composite = new ConsistentComposite();
					composite.setName(teamTextField.getText());
					int flag2 = 0, flag = 0;
					Iterator<ConsistentComponent> iterator = consistentComposite.iterator();
					while (iterator.hasNext() && (flag2 == 0 || flag == 0)) {
						ConsistentComposite temp = (ConsistentComposite) iterator.next();
						if (temp.getName().equals(composite2.getName())) {
							composite2 = temp;
							flag2 = 1;
						} else if (temp.getName().equals(composite.getName())) {
							composite = temp;
							flag = 1;
						}
					}

					if (consistentComposite.contains(composite2)) {
						if (consistentComposite.contains(composite)) {
							if (composite.getParent() == null) {
								consistentComposite.remove(composite2);
								composite2.addChild(composite);
								consistentComposite.add(composite2);
								consistentComposite.remove(composite);
								composite.setParent(composite2);
								consistentComposite.add(composite);

								textArea.appendText(
										"添加团队1 " + composite.getName() + " 到团队2 " + composite2.getName() + " 成功！");
							} else if (composite.getParent().getName().equals(composite2.getName())) {
								textArea.appendText(
										"团队1 " + composite.getName() + " 已加入团队2 " + composite2.getName() + " ，无法添加！");
							} else {
								textArea.appendText("团队1 " + composite.getName() + " 已加入其它团队 "
										+ composite.getParent().getName() + " ，无法添加！");
							}
						} else {
							textArea.appendText("团队1 " + composite.getName() + " 不存在，请先创建再进行添加操作！");
						}
					} else {
						textArea.appendText("团队2 " + composite2.getName() + " 不存在，请先创建再进行添加操作！");
					}

				} else {
					textArea.appendText("请输入两个不同的团队再进行添加操作！");
				}
			}
		});

		Button removePersonButton = new Button("删除个人");
		removePersonButton.setPrefSize(150, 25);
		removePersonButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				textArea.setText("");

				ConsistentLeaf leaf = new ConsistentLeaf();
				leaf.setName(personTextField.getText());
				Iterator<ConsistentComponent> iterator = consistentLeaf.iterator();
				while (iterator.hasNext()) {
					ConsistentLeaf temp = (ConsistentLeaf) iterator.next();
					if (temp.getName().equals(leaf.getName())) {
						leaf = temp;
						break;
					}
				}

				if (consistentLeaf.contains(leaf)) {
					consistentLeaf.remove(leaf);

					ConsistentComposite temp = null;
					for (ConsistentComponent c : consistentComposite) {
						if (leaf.getParent() != null
								&& leaf.getParent().getName().equals(((ConsistentComposite) c).getName())) {
							temp = (ConsistentComposite) c;
							consistentComposite.remove(temp);
							temp.removeChild(leaf);
							consistentComposite.add(temp);
							break;
						}
					}

					textArea.appendText("个人 " + leaf.getName() + " 删除成功！");
				} else {
					textArea.appendText("个人 " + leaf.getName() + " 不存在，无法删除！");
				}
			}
		});

		Button removeTeamButton = new Button("删除团队1");
		removeTeamButton.setPrefSize(150, 25);
		removeTeamButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				textArea.setText("");

				ConsistentComposite composite = new ConsistentComposite();
				composite.setName(teamTextField.getText());
				Iterator<ConsistentComponent> iterator = consistentComposite.iterator();
				while (iterator.hasNext()) {
					ConsistentComposite temp = (ConsistentComposite) iterator.next();
					if (temp.getName().equals(composite.getName())) {
						composite = temp;
						break;
					}
				}

				if (consistentComposite.contains(composite)) {
					if (composite.getParent() != null) {
						ConsistentComposite parent = new ConsistentComposite();
						parent.setName(composite.getParent().getName());
						Iterator<ConsistentComponent> iterator2 = consistentComposite.iterator();
						while (iterator2.hasNext()) {
							ConsistentComposite temp = (ConsistentComposite) iterator2.next();
							if (temp.getName().equals(parent.getName())) {
								parent = temp;
								break;
							}
						}

						consistentComposite.remove(parent);
						parent.removeChild(composite);
						consistentComposite.add(parent);
					}

					removeComposite(composite);
					textArea.appendText("团队1 " + composite.getName() + " 删除成功！");
				} else {
					textArea.appendText("团队1  " + composite.getName() + " 不存在，无法删除！");
				}
			}
		});

		HBox buttonHBox = new HBox(5);
		buttonHBox.setPadding(new Insets(5, 0, 0, 0));
		buttonHBox.getChildren().addAll(viewAllButton, createPersonButton, createTeamButton);

		HBox buttonHBox2 = new HBox(5);
		buttonHBox2.setPadding(new Insets(0, 0, 5, 0));
		buttonHBox2.getChildren().addAll(addPersonToTeamButton, addTeamToTeamButton, removePersonButton,
				removeTeamButton);

		vBox.getChildren().addAll(personHBox, teamHBox, team2HBox, textArea, buttonHBox, buttonHBox2);
		Scene scene = new Scene(vBox, 600, 400);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	public void viewComposite(ConsistentComponent c, int level) {
		String preStr = "";
		for (int i = 0; i < level; i++) {
			preStr += "    ";
		}

		if (level == 1) {
			textArea.appendText("+团队：" + ((ConsistentComposite) c).getName() + "\n");
		}

		if (((ConsistentComposite) c).getChildren() != null) {
			for (ConsistentComponent child : ((ConsistentComposite) c).getChildren()) {
				if (child != null) {
					if (child instanceof ConsistentComposite) {
						textArea.appendText(preStr + "+团队：" + ((ConsistentComposite) child).getName() + "\n");
						viewComposite(((ConsistentComposite) child), level + 1);
					} else {
						textArea.appendText(preStr + "-个人：" + ((ConsistentLeaf) child).getName() + "\n");
					}
				}
			}
		}
	}

	public void removeComposite(ConsistentComponent c) {
		if (((ConsistentComposite) c).getChildren() != null) {
			for (ConsistentComponent child : ((ConsistentComposite) c).getChildren()) {
				if (child != null) {
					if (child instanceof ConsistentComposite) {
						removeComposite(((ConsistentComposite) child));
					} else {
						consistentLeaf.remove((ConsistentLeaf) child);
					}
				}
			}
		}

		consistentComposite.remove(c);
	}
}
